﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace JQueryWidget.WidgetFrame
{
    public partial class EditWidget : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["pageid"] == null || Request["columnid"] == null || Request["index"] == null)
            {
                Response.Clear();
                Response.Write("Error");
                Response.End();
            }
            else
            { 
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Request["pageid"] != null && Request["columnid"] != null && Request["index"] != null)
            {
                string pageId = Request["pageid"];
                string columnid = Request["columnid"];
                int index = Convert.ToInt32(Request["index"]);

                var templatedataid = (from pagedata in XDocument.Load(Server.MapPath("~/Data/pagedata.xml")).Element("pages").Elements("page")
                                      where pagedata.Element("id").Value == pageId.ToString()
                                      select pagedata.Element("templatedataid").Value).FirstOrDefault();
                if (templatedataid != null)
                {
                    XDocument xDoc = XDocument.Load(Server.MapPath("~/Data/templatedata.xml"));
                    var templateData = (from td in xDoc.Element("templatedatas").Elements("templatedata")
                                        where td.Element("id").Value == templatedataid
                                        select td).FirstOrDefault();
                    if (templateData != null)
                    {
                        var columnData = (from cd in templateData.Element("columns").Elements("column")
                                          where cd.Attribute("id").Value == columnid
                                          select cd).FirstOrDefault();
                        if (columnData != null)
                        {
                            var widget = columnData.Element("widgets").Elements("widget").ElementAtOrDefault(index);
                            if (widget != null)
                            {
                                var widgetTitle = widget.Element("title");
                                if (widgetTitle == null)
                                {
                                    widgetTitle = new XElement("title");
                                    widget.Add(widgetTitle);
                                }
                                widgetTitle.Value = TitleTextBox.Text.Trim();
                            }
                            xDoc.Save(Server.MapPath("~/Data/templatedata.xml"));
                        }

                    }
                }
            }
        }
    }
}